# CMakeLists.txt for libgnucash/core-utils

ADD_SUBDIRECTORY(test)

IF (BUILDING_FROM_VCS)
  SET (SWIG_CORE_UTILS_GUILE_C ${CMAKE_CURRENT_BINARY_DIR}/swig-core-utils-guile.c)
  GNC_ADD_SWIG_COMMAND (swig-core-utils-guile-c ${SWIG_CORE_UTILS_GUILE_C} ${CMAKE_CURRENT_SOURCE_DIR}/core-utils.i)
  SET (SWIG_CORE_UTILS_PYTHON_C ${CMAKE_CURRENT_BINARY_DIR}/swig-core-utils-python.c)
  GNC_ADD_SWIG_PYTHON_COMMAND (swig-core-utils-python ${SWIG_CORE_UTILS_PYTHON_C} ${CMAKE_CURRENT_SOURCE_DIR}/core-utils.i)
ELSE()
  SET(SWIG_CORE_UTILS_GUILE_C swig-core-utils-guile.c)
  SET(SWIG_CORE_UTILS_PYTHON_C swig-core-utils-python.c)
ENDIF()

SET (core_utils_SOURCES
  binreloc.c
  gnc-prefs.c
  gnc-environment.c
  gnc-filepath-utils.cpp
  gnc-gkeyfile-utils.c
  gnc-glib-utils.c
  gnc-guile-utils.c
  gnc-jalali.c
  gnc-locale-utils.c
  gnc-path.c
)

# Add dependency on config.h
SET_SOURCE_FILES_PROPERTIES (${core_utils_SOURCES} PROPERTIES OBJECT_DEPENDS ${CONFIG_H})

IF (BUILDING_FROM_VCS)
  # Command to generate the swig-runtime.h header
  ADD_CUSTOM_COMMAND (
    OUTPUT ${SWIG_RUNTIME_H}
    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
    COMMAND ${SWIG_EXECUTABLE} -guile -external-runtime ${SWIG_RUNTIME_H}
  )
ENDIF()

ADD_CUSTOM_TARGET(swig-runtime-h DEPENDS ${SWIG_RUNTIME_H})

# Add dependency on swig-runtime.h
SET_SOURCE_FILES_PROPERTIES (gnc-guile-utils.c PROPERTIES OBJECT_DEPENDS ${SWIG_RUNTIME_H})

SET(prefix ${CMAKE_INSTALL_PREFIX})
if(ENABLE_BINRELOC)
  SET(datadir ${DATADIR})
  SET(bindir ${BINDIR})
  SET(libdir ${LIBDIR})
  SET(sysconfdir ${SYSCONFDIR})
  SET(localedir ${LOCALEDIR})
else()
  SET(datadir ${CMAKE_INSTALL_FULL_DATADIR})
  SET(bindir ${CMAKE_INSTALL_FULL_BINDIR})
  SET(libdir ${CMAKE_INSTALL_FULL_LIBDIR})
  SET(sysconfdir ${CMAKE_INSTALL_FULL_SYSCONFDIR})
  SET(localedir "${CMAKE_INSTALL_FULL_DATAROOTDIR}/locale")
endif()
GNC_CONFIGURE(gncla-dir.h.in gncla-dir.h)

### Create gnc-version.h ###

SET (GNC_VERSION_H_IN
"/* Autogenerated. Do not change. */
#ifndef GNC_VERSION_H
#define GNC_VERSION_H

#define GNC_VCS \"git\"
#include \"gnc-vcs-info.h\"
#endif
")

STRING(CONFIGURE ${GNC_VERSION_H_IN} GNC_VERSION_H_CONTENT)
FILE (WRITE ${CMAKE_CURRENT_BINARY_DIR}/gnc-version.h ${GNC_VERSION_H_CONTENT})

### Create gnc-vcs-info.h
# This can only be done when building from a vcs (git/svn/bzr/svk) working directory.
# This file is shipped in the distribution tarball, so no need to generate it in that case anyway.
# The meta-cmake gymnastics here come from https://cmake.org/pipermail/cmake/2010-July/038015.html
IF (BUILDING_FROM_VCS)
FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/gnc-vcs-info.h.in
"/* Autogenerated. Do not change. */
#define GNC_VCS_REV \"@GNC_VCS_REV@\"
#define GNC_VCS_REV_DATE \"@GNC_VCS_REV_DATE@\"
#define GNC_VCS_REV_YEAR \"@GNC_VCS_REV_YEAR@\"
#define GNC_VCS_REV_Y_M \"@GNC_VCS_REV_Y_M@\"
")

SET(SCM_REV_COMMAND "${CMAKE_SOURCE_DIR}/util/gnc-vcs-info -r")
SET(SCM_REV_DATE_COMMAND "${CMAKE_SOURCE_DIR}/util/gnc-vcs-info -d")
FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/version.cmake
  "EXECUTE_PROCESS(
    COMMAND ${SHELL} ${SCM_REV_COMMAND} ${CMAKE_SOURCE_DIR}
    OUTPUT_VARIABLE GNC_VCS_REV
    OUTPUT_STRIP_TRAILING_WHITESPACE
  )
  EXECUTE_PROCESS(
    COMMAND ${SHELL} ${SCM_REV_DATE_COMMAND} ${CMAKE_SOURCE_DIR}
    OUTPUT_VARIABLE GNC_VCS_REV_DATE
    OUTPUT_STRIP_TRAILING_WHITESPACE
  )
  STRING(SUBSTRING \${GNC_VCS_REV_DATE} 0 4 GNC_VCS_REV_YEAR)
  STRING(SUBSTRING \${GNC_VCS_REV_DATE} 0 7 GNC_VCS_REV_Y_M)
  CONFIGURE_FILE(\${SRC} \${DST} @ONLY)
")

IF (${CMAKE_VERSION} VERSION_GREATER "3.1")
  ADD_CUSTOM_TARGET(gnc-vcs-info ALL
    BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/gnc-vcs-info.h
    COMMAND ${CMAKE_COMMAND} -D SRC=${CMAKE_CURRENT_BINARY_DIR}/gnc-vcs-info.h.in
                     -D DST=${CMAKE_CURRENT_BINARY_DIR}/gnc-vcs-info.h
                     -P ${CMAKE_CURRENT_BINARY_DIR}/version.cmake
  )
ELSE()
  # BYPRODUCTS keyword not supported below 3.2
  ADD_CUSTOM_TARGET(gnc-vcs-info ALL
    COMMAND ${CMAKE_COMMAND} -D SRC=${CMAKE_CURRENT_BINARY_DIR}/gnc-vcs-info.h.in
                     -D DST=${CMAKE_CURRENT_BINARY_DIR}/gnc-vcs-info.h
                     -P ${CMAKE_CURRENT_BINARY_DIR}/version.cmake
  )
ENDIF()
ELSE(BUILDING_FROM_VCS)
  ADD_CUSTOM_TARGET(gnc-vcs-info DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/gnc-vcs-info.h)
ENDIF(BUILDING_FROM_VCS)
### Compile library

SET(core_utils_noinst_HEADERS
  binreloc.h
  gnc-prefs.h
  gnc-prefs-p.h
  gnc-environment.h
  gnc-filepath-utils.h
  gnc-gkeyfile-utils.h
  gnc-glib-utils.h
  gnc-guile-utils.h
  gnc-jalali.h
  gnc-locale-utils.h
  gnc-path.h
)

SET(core_utils_ALL_SOURCES ${core_utils_SOURCES} ${core_utils_noinst_HEADERS})
SET(core_utils_ALL_LIBRARIES ${Boost_LIBRARIES} ${GUILE_LDFLAGS} ${GLIB2_LDFLAGS} ${GOBJECT_LDFLAGS} ${GTK_MAC_LDFLAGS})
SET(core_utils_ALL_INCLUDES
  ${CMAKE_SOURCE_DIR}/common
  ${CMAKE_BINARY_DIR}/common
  ${CMAKE_CURRENT_BINARY_DIR}
  ${CMAKE_CURRENT_SOURCE_DIR}
  ${GLIB2_INCLUDE_DIRS}
  ${GUILE_INCLUDE_DIRS}
  ${GTK_MAC_INCLUDE_DIRS})

SET_LOCAL_DIST(core_utils_DIST_local ${core_utils_ALL_SOURCES} CMakeLists.txt Makefile.am core-utils.i core-utils.scm gncla-dir.h.in)
SET(core_utils_DIST ${core_utils_DIST_local} ${test_core_utils_DIST} PARENT_SCOPE)

IF (MAC_INTEGRATION)
  LIST(APPEND core_utils_ALL_LIBRARIES ${OSX_EXTRA_LIBRARIES})
ENDIF()

ADD_LIBRARY	(gnc-core-utils ${core_utils_ALL_SOURCES} ${SWIG_CORE_UTILS_GUILE_C})
ADD_DEPENDENCIES(gnc-core-utils gnc-vcs-info)

TARGET_LINK_LIBRARIES(gnc-core-utils ${core_utils_ALL_LIBRARIES})

TARGET_COMPILE_DEFINITIONS(gnc-core-utils
    PRIVATE -DG_LOG_DOMAIN=\"gnc.core-utils\" ${GTK_MAC_CFLAGS_OTHER})

TARGET_INCLUDE_DIRECTORIES(gnc-core-utils PUBLIC ${core_utils_ALL_INCLUDES})

IF (MAC_INTEGRATION)
  TARGET_COMPILE_OPTIONS(gnc-core-utils PRIVATE ${OSX_EXTRA_COMPILE_FLAGS})
ENDIF(MAC_INTEGRATION)

INSTALL(TARGETS gnc-core-utils
  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

IF (WITH_PYTHON)
  ADD_LIBRARY	(gnc-core-utils-python ${core_utils_ALL_SOURCES} ${SWIG_CORE_UTILS_PYTHON_C})
  ADD_DEPENDENCIES(gnc-core-utils-python gnc-vcs-info)

  TARGET_LINK_LIBRARIES(gnc-core-utils-python ${core_utils_ALL_LIBRARIES} ${PYTHON_LIBRARIES})

  TARGET_COMPILE_DEFINITIONS(gnc-core-utils-python
    PRIVATE -DG_LOG_DOMAIN=\"gnc.core-utils\" ${GTK_MAC_CFLAGS_OTHER}
  )

  TARGET_INCLUDE_DIRECTORIES(gnc-core-utils-python PUBLIC ${core_utils_ALL_INCLUDES} ${PYTHON_INCLUDE_DIRS})

  IF (MAC_INTEGRATION)
    TARGET_COMPILE_OPTIONS(gnc-core-utils-python PRIVATE ${OSX_EXTRA_COMPILE_FLAGS})
  ENDIF(MAC_INTEGRATION)

  INSTALL(TARGETS gnc-core-utils-python
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  )

ENDIF()

# No headers to install

# Scheme

SET (core_utils_SCHEME core-utils.scm)

SET(GUILE_OUTPUT_DIR   gnucash)
SET(GUILE_DEPENDS      gnc-core-utils)

GNC_ADD_SCHEME_TARGETS(scm-core-utils
    "${core_utils_SCHEME}"
    ${GUILE_OUTPUT_DIR}
    "${GUILE_DEPENDS}"
    FALSE
)

